
#include "api_server_client.h"
#include <iostream>
#include "pod_parser.h"
#include "node_parser.h"
#include "task_parser.h"
#include "http_client.h"

namespace nanok {

int getPods(std::vector<Pod>& pods) {
    // 创建 HTTP 客户端
    HttpClient client;
    // 发送 GET 请求到 API Server
    std::string url = "http://localhost:8090/pods";
    std::string response = client.Get(url);
    if (response.empty()) {
        std::cerr << "Failed to get response(pods) from API Server" << std::endl;
        return 1;
    }
    PodParser::ParsePodList(response, pods);
    return 0;
}

int getNodes(std::vector<Node>& nodes) {
    // 创建 HTTP 客户端
    HttpClient client;
    // 发送 GET 请求到 API Server
    std::string url = "http://localhost:8090/nodes";
    std::string response = client.Get(url);
    if (response.empty()) {
        std::cerr << "Failed to get response(nodes) from API Server" << std::endl;
        return 1;
    }
    NodeParser::ParseNodeList(response, nodes);
    return 0;
}

int getTasks(std::vector<Task>& tasks) {
    // 创建 HTTP 客户端
    HttpClient client;
    // 发送 GET 请求到 API Server
    std::string url = "http://localhost:8090/tasks";
    std::string response = client.Get(url);
    if (response.empty()) {
        std::cerr << "Failed to get response(getTasks) from API Server" << std::endl;
        return 1;
    }
    TaskParser::ParseTaskList(response, tasks);
    return 0;
}

int submitTask(const std::string& key, const std::string& value) {
    std::ostringstream oss;
    oss << "{\"key\":\"" << key << "\", \"value\":" << value << "}";
    std::string taskJson = oss.str();
    std::string url = "http://localhost:8090/submitTask";
    // 创建 HTTP 客户端
    HttpClient client;
    std::string response = client.Post(url, taskJson);
    if (response.empty()) {
        std::cerr << "Failed to get response from API Server for submitTask" << std::endl;
        return 1;
    }
    return 0;
}

int updateTask(const std::string& key, const std::string& value) {
    std::ostringstream oss;
    oss << "{\"key\":\"" << key << "\", \"value\":" << value << "}";
    std::string taskJson = oss.str();
    std::string url = "http://localhost:8090/updateTask";
    // 创建 HTTP 客户端
    HttpClient client;
    std::string response = client.Post(url, taskJson);
    if (response.empty()) {
        std::cerr << "Failed to get response from API Server for updateTask" << std::endl;
        return 1;
    }
    return 0;
}

int getPod(const std::string& podKey, Pod& pod) {
    std::string url = "http://localhost:8090/pods/" + podKey;
    // 创建 HTTP 客户端
    HttpClient client;
    std::string response = client.Get(url);
    if (response.empty()) {
        std::cerr << "Failed to get response from API Server for getPod" << std::endl;
        return 1;
    }
    PodParser::ParsePod(response, pod);
    return 0;
}

int updatePod(const std::string& key, const std::string& value) {
    std::ostringstream oss;
    oss << "{\"key\":\"" << key << "\", \"value\":" << value << "}";
    std::string podJson = oss.str();
    std::string url = "http://localhost:8090/updatePod";
    // 创建 HTTP 客户端
    HttpClient client;
    std::string response = client.Post(url, podJson);
    if (response.empty()) {
        std::cerr << "Failed to get response from API Server for getPod" << std::endl;
        return 1;
    }
    return 0;
}

int registerNode(const std::string& key, const std::string& value) {
    std::ostringstream oss;
    oss << "{\"key\":\"" << key << "\", \"value\":" << value << "}";
    std::string nodeJson = oss.str();
    std::string url = "http://localhost:8090/registerNode";
    // 创建 HTTP 客户端
    HttpClient client;
    std::string response = client.Post(url, nodeJson);
    if (response.empty()) {
        std::cerr << "Failed to get response from API Server for getPod" << std::endl;
        return 1;
    }
    return 0;    
}

}